<?php
/**
 * Created by PhpStorm.
 * User: jingshou
 * Date: 2017/8/27
 * Time: 22:21
 */

namespace app\admin\controller;

use app\admin\model\User as UserModel;
use think\Request;
use think\Url;

class User extends Admin
{
    public function index()
    {
        $list = UserModel::userList([]);
        return $this->fetch('list', [
            'list' => $list,
        ]);
    }

    public function add()
    {
        $model = UserModel::get(0);
        if (Request::instance()->isPost()) {
            $data = Request::instance()->post();
            $res = UserModel::add($data);
            return $res;
        }
        return $this->fetch('add', [
            'model' => $model,
        ]);
    }

    public function detail()
    {

        if (Request::instance()->isPost()) {
            $data = Request::instance()->post();
            $res = UserModel::updateUser($data);
            return $res;
        }
        $id = Request::instance()->param('id');
        $model = UserModel::get($id);
        return $this->fetch('add', ['model' => $model]);
    }

    public function del($id)
    {
        $res = UserModel::del($id);
        if ($res == 0) {
            return resError('删除失败');
        } else {
            return resData('删除成功');
        }
    }
}
